﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class CartdetailDAO:IData 
 	{
		public CartdetailDAO(String myconnecString) : base(myconnecString) { }

		public  int OnSubmit(Cartdetail item, int t) {
			 return connec.ExecuteNonQuery("sp_Cartdetail", CommandType.StoredProcedure,new SqlParameter("@flag", t),
				 new SqlParameter("@" + Cartdetail.c_id, item.id), new SqlParameter("@" + Cartdetail.c_cartId, item.cartId),
				 new SqlParameter("@" + Cartdetail.c_productId, item.productId), new SqlParameter("@" + Cartdetail.c_quality, item.quality),
				 new SqlParameter("@" + Cartdetail.c_unitPriceUSD, item.unitPriceUSD), new SqlParameter("@" + Cartdetail.c_rate, item.rate),
				 new SqlParameter("@" + Cartdetail.c_unitPriceVND, item.unitPriceVND), new SqlParameter("@" + Cartdetail.c_discount, item.discount),
				 new SqlParameter("@" + Cartdetail.c_cash, item.cash), new SqlParameter("@" + Cartdetail.c_orderId, item.orderId)); 
		}

		private  Cartdetail GetItem(IDataReader dr)
		{
			Cartdetail obj = new Cartdetail();
			obj.id = Convert.ToInt64(dr[Cartdetail.c_id]);
			obj.cartId = Convert.ToInt64(dr[Cartdetail.c_cartId]);
			obj.productId = Convert.ToInt32(dr[Cartdetail.c_productId]);
			obj.quality = Convert.ToInt32(dr[Cartdetail.c_quality]);
			obj.unitPriceUSD = Convert.ToDecimal(dr[Cartdetail.c_unitPriceUSD]);
			obj.rate = Convert.ToDecimal(dr[Cartdetail.c_rate]);
			obj.unitPriceVND = Convert.ToDecimal(dr[Cartdetail.c_unitPriceVND]);
			obj.discount = Convert.ToDecimal(dr[Cartdetail.c_discount]);
			obj.cash = Convert.ToDecimal(dr[Cartdetail.c_cash]);
			obj.orderId = Convert.ToInt32(dr[Cartdetail.c_orderId]);
			return obj;
		}

        private Cartdetail Item(IDataReader dr)
        {
            Cartdetail obj = new Cartdetail();
            obj.id = Convert.ToInt64(dr[Cartdetail.c_id]);
            obj.cartId = Convert.ToInt64(dr[Cartdetail.c_cartId]);
            obj.productId = Convert.ToInt32(dr[Cartdetail.c_productId]);
            obj.quality = Convert.ToInt32(dr[Cartdetail.c_quality]);
            obj.unitPriceUSD = Convert.ToDecimal(dr[Cartdetail.c_unitPriceUSD]);
            obj.rate = Convert.ToDecimal(dr[Cartdetail.c_rate]);
            obj.unitPriceVND = Convert.ToDecimal(dr[Cartdetail.c_unitPriceVND]);
            obj.discount = Convert.ToDecimal(dr[Cartdetail.c_discount]);
            obj.cash = Convert.ToDecimal(dr[Cartdetail.c_cash]);
            obj.orderId = Convert.ToInt32(dr[Cartdetail.c_orderId]);
            obj.productName = dr[Products.c_productName].ToString();
            return obj;
        }

        //Lay chi tiet san pham theo gio hang
        public List<Cartdetail> GetAllByCartDetailByCartId(long cartId)
        {
            List<Cartdetail> list = new List<Cartdetail>();
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "sp_getCartDetailByCartId";
            SqlParameter param = new SqlParameter("@cartid", SqlDbType.BigInt);
            param.Value = cartId;
            command.Parameters.Add(param);
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    list.Add(this.Item(reader));
                }
                reader.Close();
            }
            connection.Close();
            return list;
        }

		public  List<Cartdetail> GetAll()
		{
			return GetAll("select * from " + Cartdetail.b_Cartdetail, CommandType.Text);
		}

        private List<Cartdetail> GetAll(String strSQL, CommandType commandtype)
        {
            List<Cartdetail> list = new List<Cartdetail>();
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = commandtype;
            command.CommandText = strSQL;
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    list.Insert(0, GetItem(reader));
                }
                reader.Close();
            }
            connection.Close();
            return list;
        }
		public  Cartdetail GetItemById(object id)
		{
			 return this.GetAll().FirstOrDefault(x => x.id == (long)id);
		}

	}

 }
